<?php
namespace app\Admin\controller;

//引入公共控制器
use app\common\controller\Backend;

//引入数据库
use think\Db;

use think\Request;

class Config extends Backend {
    //默认配置

    public function __construct() {

        // 继承父类
        parent::__construct();

        // 引入模型
        $this->ConfigModel = model( 'Config' );

    }

    /* 配置主界面 */

    public function index() {
        // 获取配置表的数据数量
        $count = $this->ConfigModel->count();

        // 获取配置表信息
        $configList = $this->ConfigModel->paginate( 10 );

        // 分页
        $pagestr = $configList->render();

        $this->assign( [
            'count' => $count,
            'configList' => $configList,
            'pagestr' => $pagestr,
        ] );

        return $this->fetch();
    }

    /* 添加配置 */

    public function add() {
        if ( $this->request->isPost() ) {

            // 获取表单信息
            $title = $this->request->param( 'title', '' );
            $key = $this->request->param( 'key', '' );
            $type = $this->request->param( 'type', '' );

            // 判断上传信息类型
            if ( $type == 'text' ) {
                $value = $this->request->param( 'value', '' );
            } else {

                // 图片上传
                $valueUrl = upload( 'value' );

                if ( $valueUrl === false ) {
                    $this->error( '文件上传失败！' );
                    exit;
                }

                $value = '/'.'uploads/'.$valueUrl;
            }

            // 组装配置信息
            $data = [
                'title' => $title,
                'key' => $key,
                'type' => $type,
                'value' => $value,
            ];

            // 配置表插入数据
            $result = $this->ConfigModel->save( $data );

            if ( $result === false ) {
                // 插入失败
                // 上传上来的图片删除了
                @is_file( $data[ 'value' ] ) && @unlink( $data[ 'value' ] );

                $this->error( $this->ConfigModel->getError() );
            } else {
                $this->success( '配置信息添加成功！', 'admin/config/index' );
                exit;
            }
        }

        return $this->fetch();
    }

    /* 修改配置 */

    public function edit() {

        // 获取url传来的数据
        $configId = $this->request->param( 'id', 0 );

        // 根据id获取配置信息
        $configInfo = $this->ConfigModel->find( $configId );

        // 渲染到界面
        $this->assign( 'configInfo', $configInfo );

        if ( $this->request->isPost() ) {

            // 获取表单信息
            $title = $this->request->param( 'title', '' );
            $key = $this->request->param( 'key', '' );
            $type = $this->request->param( 'type', '' );

            // 判断上传信息类型
            if ( $type == 'text' ) {
                $value = $this->request->param( 'value', '' );
            } else {

                // 图片上传
                $valueUrl = upload( 'value' );

                if ( $valueUrl === false ) {
                    $this->error( '文件上传失败！' );
                    exit;
                }

                $value = '/'.'uploads/'.$valueUrl;
            }

            // 组装配置信息
            $data = [
                'id' => $configId,
                'title' => $title,
                'key' => $key,
                'type' => $type,
                'value' => $value,
            ];

            // 配置表插入数据
            $result = $this->ConfigModel->isUpdate()->save( $data );

            if ( $result === false ) {
                // 插入失败
                // 上传上来的图片删除了

                if ( $type == 'file' && $_FILES[ 'value' ][ 'size' ] > 0 ) {
                    @is_file( $data[ 'value' ] ) && @unlink( $data[ 'value' ] );
                }

                $this->error( $this->ConfigModel->getError() );
            } else {

                if ( $type == 'file' && $_FILES[ 'value' ][ 'size' ] > 0 ) {
                    $avatar = ltrim( $configInfo[ 'value' ], '/' );
                    @is_file( $avatar ) && @unlink( $avatar );
                }

                $this->success( '配置信息修改成功！', 'admin/config/index' );
                exit;
            }
        }

        return $this->fetch();

    }

    /* 删除配置 */

    public function del() {
        if ( $this->request->isPost() ) {
            // 提示信息
            $success = [
                'result' => false,
                'msg' => '',
            ];

            $ids = $this->request->param( 'ids', 0 );

            // 软删除
            $result = $this->ConfigModel->destroy( $ids );

            if ( $result === FALSE ) {
                $success[ 'result' ] = false;
                $success[ 'msg' ] = $this->ConfigModel->getError();
            } else {
                $success[ 'result' ] = true;
                $success[ 'msg' ] = '删除配置成功';
            }

            return json( $success );
        }
    }

    /* 回收站 */

    public function recycle() {

        // 获取关键字
        $keywords = $this->request->param( 'keywords', '' );

        // 条件集合
        $where = [];

        if ( $keywords ) {
            // 加入条件 关键字
            $where[ 'title' ] = [ 'like', "%$keywords%" ];
        }

        // 全部配置的数量
        $configAll = $this->ConfigModel->onlyTrashed()->select();

        $configCount = count( $configAll );

        // 获取全部用户
        $configList = $this->ConfigModel->onlyTrashed()->where( $where )->order( 'id', 'desc' )->paginate( 10, false, [ 'query' => Request::instance()->param() ] );

        // 获取分页显示
        $configListstr = $configList->render();

        $this->assign( [
            'configList' => $configList,
            'configListstr' => $configListstr,
            'configCount' => $configCount,
            'keywords' => $keywords,
        ] );
        return $this->fetch();
    }

    // 真正删除

    public function cut() {
        if ( $this->request->isPost() ) {
            // 提示信息
            $success = [
                'result' => false,
                'msg' => '',
            ];

            $ids = $this->request->param( 'ids', 0 );

            $where = 'id in('.$ids.')';

            // 真删除
            $result = $this->ConfigModel->onlyTrashed()->where( $where )->delete();

            if ( $result === FALSE ) {
                $success[ 'result' ] = false;
                $success[ 'msg' ] = $this->ConfigModel->getError();
            } else {
                $success[ 'result' ] = true;
                $success[ 'msg' ] = '删除配置成功';
            }

            return json( $success );
        }
        return '456';
    }

    // 取消软删除 恢复正常

    public function recovery() {
        if ( $this->request->isPost() ) {
            // 提示信息
            $success = [
                'result' => false,
                'msg' => '',
            ];

            $ids = $this->request->param( 'ids', 0 );

            $where = 'id in('.$ids.')';

            $data = [
                'deletetime' => null,
            ];

            // 软删除
            $result = $this->ConfigModel->onlyTrashed()->where( $where )->update( [ 'deletetime' => NULL ] );

            if ( $result === FALSE ) {
                $success[ 'result' ] = false;
                $success[ 'msg' ] = $this->ConfigModel->getError();
            } else {
                $success[ 'result' ] = true;
                $success[ 'msg' ] = '恢复配置成功';
            }

            return json( $success );
        }

    }
}
